package com.example.weatherforecast;

import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weatherforecast.citypagetool.CityData;
import com.example.weatherforecast.citypagetool.CityDataManager;
import com.example.weatherforecast.citypagetool.RecommendCityAdapter;
import com.example.weatherforecast.citypagetool.Cities;
import com.example.weatherforecast.citypagetool.ReadCityUtils;
import com.hxc.basemodule.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：coderChen
 * @Project：WeatherForecast
 * Explain:搜索框以及城市列表添加指定城市
 */

public class AddCityActivity extends BaseActivity {
    private Map<String, String> cityData;
    private SharedPreferences sharedPreferences;
    private RecyclerView recyclerView;
    private RecommendCityAdapter adapter;
    private List<CityData> cities;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_city);
//        设置沉浸式窗口
        setImmersiveWindows();
//        标题栏返回上一页
        Toolbar toolbar = findViewById(R.id.AddCityTitle);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish(); // 关闭当前的 AddCityActivity
            }
        });

//        加载city.json中的城市数据
        cityData = ReadCityUtils.loadCityData(getResources().openRawResource(R.raw.city));
//        初始化SharedPreferences
        sharedPreferences = getSharedPreferences("CityData", Context.MODE_PRIVATE);
        Log.d("sharedPreferences", "sharedPreferences: " + cityData);

        SearchView searchView = findViewById(R.id.CitySearchView);
        searchView.setQueryHint("搜索城市（中文）");
        searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchView.setIconified(false);
            }
        });
//                   搜索城市时的处理逻辑
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 用户提交搜索
                if (cityData.containsKey(query)) {
                    if (CityDataManager.addCityIfNotExists(query, cityData.get(query), sharedPreferences,AddCityActivity.this)) {
                        Toast.makeText(AddCityActivity.this, "城市添加成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(AddCityActivity.this, "该城市已存在", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(AddCityActivity.this, "该城市不存在", Toast.LENGTH_SHORT).show();
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // 用户输入文本变化时
                return false;
            }
        });
        // 初始化 RecyclerView、适配器和布局管理器
        recyclerView = findViewById(R.id.cityList);
        cities = new ArrayList<>(); // 初始化城市列表
        adapter = new RecommendCityAdapter(cities,sharedPreferences); // 为 RecyclerView 创建适配器
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); // 使用每行三列的网格布局
        cities.addAll(Cities.getCommonlyUsedCities());
        adapter.notifyDataSetChanged();
    }
        @Override
    protected int getLayoutId() {
        return R.layout.activity_add_city;
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}